﻿
/// <reference path="../jquery.min-vsdoc.js" />

(function($) {
    $.fn.podMenu = function(opt) {

        var opts = $.extend({}, $.fn.podMenu.defaults, opt);

        var pos = getAbsPosition($(this)[0]);

        opts.content.width(opts.width)
        .addClass("pod-menu ui-widget ui-widget-content ui-corner-all")
        .css("top", pos.top + $(this).height()).css("left", pos.left).hide().children("ul").addClass("first-level")
        .append("<div class='ui-state-default back'><a href='###'>back</a></div>");

        $("ul", opts.content.children("ul")).hide().width(opts.width);

        //li的样式
        $("li", opts.content.children("ul")).addClass("ui-corner-all");

        //设定拥有ul的li样式
        $("li ul", opts.content).each(function() {
            $(this).parent().children("a").append('<span class="ui-icon ui-icon-triangle-1-e" ></span>');
        });

        $("li a", opts.content.children("ul")).click(function(args) {
            var a = $(this).parent().children("ul");
            if (a.size() != 0) {
                $(".show", opts.content).removeClass("show");
                a.show("slide", { direction: "right" }, 100).addClass("show");
            } else if (opts.click != false) {
                if (!opts.click($(this))) {
                    reset(opts);
                }
            }
            $(this).parent().siblings("li").addClass("ui-helper-hidden-accessible");
            $(this).hide();
            return false;
        }).hover(function() { $(this).addClass("ui-state-hover"); }, function() { $(this).removeClass("ui-state-hover"); });

        $(this).click(function() {
            reset(opts);
            opts.content.show("slide", { direction: "up" }, 100);
            return false;
        });

        $(".back", opts.content).click(function() {
            var a = $(".show", opts.content).hide()
            a.parent().closest("ul").children("li").removeClass("ui-helper-hidden-accessible");
            a.prev("a").show();
        });
    };

    $.fn.podMenu.defaults = {
        width: 200,
        height: 150,
        content: false,
        click: false// function click opts
    };



    reset = function(opts) {
        $("ul", opts.content.children("ul")).hide().width(opts.width);
        opts.content.hide();
    }



    function getAbsPosition(obj) {
        return { left: getAbsoluteLeft(obj), top: getAbsoluteTop(obj) };
    }

    function getAbsoluteLeft(o) {

        var oLeft = o.offsetLeft
        while (o.offsetParent != null) {
            oParent = o.offsetParent;
            oLeft += oParent.offsetLeft;
            o = oParent;
        }
        return oLeft;
    }

    function getAbsoluteTop(o) {

        var oTop = o.offsetTop
        while (o.offsetParent != null) {
            oParent = o.offsetParent;
            oTop += oParent.offsetTop;
            o = oParent;
        }
        return oTop;
    }


})($);

